package leetcode.leetcode_3;

import java.util.HashSet;
import java.util.Set;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        int left = 0;
        int right = 0;
        Set<Character> set = new HashSet<>();
        while(right < n){
            char c = s.charAt(right);
            if (set.contains(c)){
                ans = Math.max(ans,set.size());
                while(set.contains(c)){
                    set.remove(s.charAt(left));
                    left++;
                }
            }
            set.add(c);
            right++;
        }

        return Math.max(ans,set.size());
    }

}